var stepMessage = function(obj, message, css){
	//remove class image
	$.map(obj, function(n){
		$("span[id='" + n + "']").removeClass('message').removeClass('error');
	});	
	//remove corresponding image
	$.map(obj, function(n){
		$("img[id='" + n + "']").attr('src', '../images/blank.jpg');			
	});	
	$.map(obj, function(n){
		$("span[id='" + n + "']").html(message).addClass(css);			
	});	
	return false;
}


var imageLoader = function(obj){
	//remove corresponding span message
	$.map(obj, function(n){
		$("span[id='" + n + "']").html('');			
	});	
	$.map(obj, function(n){
		$("img[id='" + n + "']").attr('src', "../images/rotator.gif");
	});
	return false;
}



function adTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['AD'],
		Manual : 1,
		api : 'termination-api.cfm?api=ad'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) { jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});	
}


function HomeFolderTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['HomeFolder'],
		Manual : 1,
		api : 'termination-api.cfm?api=home_folder'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function HRTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['HR'],
		Manual : 1,
		api : 'termination-api.cfm?api=hr'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function TimeKeeperTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['TimeKeeper'],
		Manual : 1,
		api : 'termination-api.cfm?api=time_keeper'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function ExportMailTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['ExportMail'],
		Manual : 1,
		api : 'termination-api.cfm?api=export_mail'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function UserAccountTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['UserAccount'],
		Manual : 1,
		api : 'termination-api.cfm?api=user_account'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function RosettaTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['Rosetta'],
		Manual : 1,
		api : 'termination-api.cfm?api=rosetta'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'message');
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function CompletedTermination(){
	var jsonData = {};
	var dataObj = {
		RosettaID : $("#RosettaID").val(), 
		TermReqID : $("#TermReqID").val(),
		section : ['Completetion'],
		Manual : 1,
		api : 'termination-api.cfm?api=completion'
	};
	
	imageLoader( dataObj.section );

	$.ajax({
		type:'POST', url:dataObj.api, dataType: 'json', 
		data: dataObj, success: function(json) {jsonData = json; },
		complete: function(){
			if( jsonData['STATUS'] == 'Completed' ){				
				$("#Notice").html('Termination is complete.').removeClass('error').addClass('message');
				return false;
			}else if( jsonData['STATUS'] == 'Fail' ){				
				stepMessage( dataObj.section, jsonData['MESSAGE'], 'error');		
			}else{stepMessage( dataObj.section, 'Process failed without meaningful message', 'error')}
		}
	});
}


function bindElements(){
	$("input[id='AD']").click(function(){
		stepMessage( ['AD'], 'Waiting...', 'error' );
		adTermination();
		$(this).hide();
	});
	
	$("input[id='HomeFolder']").click(function(){
		stepMessage( ['HomeFolder'], 'Waiting...', 'error' );
		HomeFolderTermination();
		$(this).hide();
	});
	
	$("input[id='HR']").click(function(){
		stepMessage( ['HR'], 'Waiting...', 'error' );
		HRTermination();
		$(this).hide();
	});
	
	$("input[id='TimeKeeper']").click(function(){
		stepMessage( ['TimeKeeper'], 'Waiting...', 'error' );
		TimeKeeperTermination();
		$(this).hide();
	});
	
	$("input[id='ExportMail']").click(function(){
		stepMessage( ['ExportMail'], 'Waiting...', 'error' );
		ExportMailTermination();
		$(this).hide();
	});
	
	$("input[id='UserAccount']").click(function(){
		stepMessage( ['UserAccount'], 'Waiting...', 'error' );
		UserAccountTermination();
		$(this).hide();
	});
	
	$("input[id='Rosetta']").click(function(){
		stepMessage( ['Rosetta'], 'Waiting...', 'error' );
		RosettaTermination();
		$(this).hide();
	});
	
    $("input[id='Complete']").click(function(){
		stepMessage( ['Complete'], 'Waiting...', 'error' );
		CompletedTermination();
	});

}



$(document).ready(function(){
	bindElements();

});